import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-10-07  19:42
 */
public class Group {
    private Member[] members;
    private int size;

    public Group(int capacity) {
        members = new Member[capacity];
        size = 0;
    }

    public void addMember(Member member) {
        if (size >= members.length) {
            System.out.println("群组已满，不能添加新成员！");
            return;
        }
        members[size++] = member;
    }

    public void deleteMember(String name) {
        for (int i = 0; i < size; i++) {
            if (members[i].getName().equals(name)) {
                for (int j = i; j < size - 1; j++) {
                    members[j] = members[j + 1];
                }
                members[--size] = null;
                return;
            }
        }
        System.out.println("成员未找到！");
    }

    public Member findMemberByName(String name) {
        for (int i = 0; i < size; i++) {
            if (members[i].getName().equals(name)) {
                return members[i];
            }
        }
        return null;
    }

    public void updateMemberInfo(String name, Member updatedMember) {
        for (int i = 0; i < size; i++) {
            if (members[i].getName().equals(name)) {
                members[i] = updatedMember;
                return;
            }
        }
        System.out.println("成员未找到！");
    }

    public void listAllMembers() {
        for (int i = 0; i < size; i++) {
            System.out.println(members[i]);
        }
    }

    public void sortByAge() {
        Arrays.sort(members, 0, size, Comparator.comparingInt(Member::getAge));
    }

    public void sortByName() {
        Arrays.sort(members, 0, size, Comparator.comparing(Member::getName));
    }

    public int totalMembers() {
        return size;
    }

    public Member findMemberId(String id) {
        for (int i = 0; i < size; i++) {
            if (members[i].getId().equals(id)) {
                return members[i];
            }
        }
        return null;
    }

    public void clearAllGroup() {
        for (int i = 0; i < size; i++) {
            members[i] = null;
        }
        size = 0;
    }
}
